from sqlalchemy.orm import Session

from controller.PublicAdminController import PublicAdminController
from controller.UserAdminController import UserAdminController
from crud import StudentAdminCrud
from schema.ParamSchema import UserInfo
from schema.QuerySchema import QueryBase, StudentQuery
from schema.TeachingSchema import StudentInfo
from util.EmailHelper import EmailHelper
from util.ErrorCode import ErrorCode


class StudentAdminController:

    @staticmethod
    def add_student(studentInfo: StudentInfo, db: Session):
        return StudentAdminCrud.add_student(studentInfo, db)

    @staticmethod
    def mod_student(studentInfo: StudentInfo, db: Session):
        return StudentAdminCrud.mod_student(studentInfo, db)

    @staticmethod
    def query_student_by_field(studentQuery: StudentQuery, db: Session):
        return StudentAdminCrud.query_student_by_field(studentQuery, db)

    @staticmethod
    def query_student_by_class_id(class_id: int, db: Session):
        return StudentAdminCrud.query_student_by_class_id(class_id, db)

    @staticmethod
    def query_student_by_id(student_id: int, db: Session):
        return StudentAdminCrud.query_student_by_id(student_id, db)

    @staticmethod
    def query_student_by_phone_and_classid(phone: str, class_id: int, db: Session):
        return StudentAdminCrud.query_student_by_phone_and_classid(phone, class_id, db)

    @staticmethod
    def query_student_by_page(query_base: QueryBase, db: Session):
        return StudentAdminCrud.query_student_by_page(query_base, db)

    @staticmethod
    def set_monitor(student_id: int, is_monitor: int, db: Session):
        response = StudentAdminCrud.set_monitor(student_id, is_monitor, db)
        if response.get('code') == 0:
            studentModel = response.get('data')
            if studentModel is None:
                return {'code': ErrorCode.SUCCESS.value, 'message': 'success'}
            else:
                response = UserAdminController.query_user_by_userid(studentModel.phone, db)
                if response.get('code') == 0:
                    userModel = response.get('data')
                    if userModel is None:
                        password = PublicAdminController.generate_password()
                        userInfo = UserInfo(user_id=studentModel.phone, port_id=6, user_name=studentModel.name,
                                            password=password, status=1)
                        response = UserAdminController.add_user(userInfo, db)

                        if response.get('code') == 0:
                            if EmailHelper.sendEmail(studentModel.email, '', '用户创建成功通知',
                                                     f'用户名:{studentModel.phone}\n密码:{password}'):
                                return {'code': ErrorCode.SUCCESS.value, 'message': 'success'}
                            else:
                                return {'code': ErrorCode.SEND_EMAIL_ERROR.value,
                                        'message': 'create user success! but sendemail failed! please contact administrator!'}
                        else:
                            return {'code': ErrorCode.CREATE_USER_FAILED.value,
                                    'message': 'create user failed! please contact administrator!'}
                    else:
                        if is_monitor == 1:
                            user_status = 1
                        else:
                            user_status = 2

                        userInfo = UserInfo(user_id=studentModel.phone, status=user_status)
                        response = UserAdminController.mod_user(userInfo, db)
                        return response
                else:
                    return {'code': ErrorCode.STUDENT_MOD_SUCCESS_BUT_USER_MOD_FAILED.value,
                            'message': 'student mod success,but user mod failed! please contact administrator!'}
        else:
            return response
